package edu.njucm.herbiv.service.impl;

import edu.njucm.herbiv.dao.ChemicalsRepository;
import edu.njucm.herbiv.pojo.entity.Chemicals;
import edu.njucm.herbiv.service.ChemicalsService;
import edu.njucm.herbiv.utils.PageUtils;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @Author ZhouHang
 * @Date 24/4/19 019
 */
@Service
public class ChemicalsServiceImpl implements ChemicalsService {
    @Resource
    private ChemicalsRepository chemicalsRepository;

    @Override
    public Page<Chemicals> list(String type, String content, int page, int size) {
        String searchContent = "%" + content + "%";
        Pageable pageable = PageUtils.getPage(page, size, "id");

        return switch (type) {
            case "id" -> chemicalsRepository.findAllByIdLike(searchContent, pageable);
            case "name" -> chemicalsRepository.findAllByNameLike(searchContent, pageable);
            default -> chemicalsRepository.findAll(pageable);
        };
    }
}
